﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Drawing;

namespace QLBH.StandardUI
{
    public class DataGridViewEx:DataGridView
    {
        public bool CheckAll
        {
            set
            {
                checkboxHeader_CheckedChanged(null, null);
            }
        }
        public void InitCheckboxColumnHeader()
        { // customize dataviewgrid, add checkbox column
            DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
            checkboxColumn.Width = 30;
            checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.Columns.Insert(0, checkboxColumn);
            checkboxColumn.Name = "";

            // add checkbox header
            Rectangle rect = this.GetCellDisplayRectangle(0, -1, true);
            // set checkbox header to center of header cell. +1 pixel to position correctly.
            rect.X = rect.Location.X + (this.Columns[0].Width - 18) / 2 + 1;
            rect.Y = rect.Location.Y + (this.ColumnHeadersHeight - 18) / 2;

            CheckBox checkboxHeader = new CheckBox();
            checkboxHeader.Name = "checkboxHeader";
            checkboxHeader.Size = new Size(18, 18);
            checkboxHeader.Location = rect.Location;
            checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);

            this.Controls.Add(checkboxHeader);
            
        }
        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.RowCount; i++)
            {
                this[0, i].Value = ((CheckBox)this.Controls.Find("checkboxHeader", true)[0]).Checked;
            }
            this.EndEdit();
        }
        public DataGridViewEx()
            :base()
        {
            this.BackgroundColor=Color.Gray;

            this.CellContentClick += new DataGridViewCellEventHandler(DataGridViewEx_CellContentClick);
            this.CellClick += new DataGridViewCellEventHandler(DataGridViewEx_CellClick);
        }

        void DataGridViewEx_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Columns[e.ColumnIndex].Name == "")
            {
                DataGridViewCheckBoxCell cell = this.CurrentCell as DataGridViewCheckBoxCell;

                if (cell.Selected == false)
                {
                    ((CheckBox)this.Controls.Find("checkboxHeader", true)[0]).Checked = false;
                }
                
            }
        }

        void DataGridViewEx_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 0 && this.Columns[e.ColumnIndex].Name == "")
            {
                DataGridViewCheckBoxCell cell = this.CurrentCell as DataGridViewCheckBoxCell;

                if (cell.Selected == false)
                {
                    ((CheckBox)this.Controls.Find("checkboxHeader", true)[0]).Checked = false;
                }
            }
        }
        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush b = new SolidBrush(RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString(e.RowIndex.ToString(), e.InheritedRowStyle.Font, b,
                    e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);

            }
            base.OnRowPostPaint(e);
        }

        
    }
}
